$(function () {
    initMovieManageTool(); //建立Movie管理对象
    tinyMCE.init({
        selector: "#movie_movieDesc",
        theme: 'advanced',
        missingMessage: '请输入影片简介',
        language: "zh",
        required: true,
        strict_loading_mode: 1,
    });

    $("#movie_manage").datagrid({
        url: '/Movie/list',
        queryParams: {
            "csrfmiddlewaretoken": $('input[name="csrfmiddlewaretoken"]').val()
        },
        fit: true,
        fitColumns: true,
        striped: true,
        rownumbers: true,
        border: false,
        pagination: true,
        pageSize: 20,
        pageList: [5, 10, 20, 30, 40, 50],
        pageNumber: 1,
        sortName: "movieId",
        sortOrder: "desc",
        toolbar: "#movie_manage_tool",
        columns: [[
            {
                field: "title",
                title: "影片名称",
                width: 200,
            },
            {
                field: "genres",
                title: "影片体裁",
                width: 100,
            },
            {
                field: "movieYear",
                title: "上映年份",
                width: 40,
            },
            {
                field: "movieDesc",
                title: "影片简介",
                width: 200,
            },
        ]],
    });

    $("#movieAddDiv").dialog({
        title: "修改管理",
        top: "10px",
        width: 1000,
        height: 600,
        modal: true,
        closed: true,
        iconCls: "icon-edit-new",
        buttons: [{
            text: "提交",
            iconCls: "icon-edit-new",
            handler: function () {
                if ($("#movieAddForm").form("validate")) {
                    if (!$("#movieAddForm").form("validate")) {
                        $.messager.alert("错误提示", "你输入的信息还有错误！", "warning");
                    } else {
                        $("#movieAddForm").form({
                            url: "/Movie/update/" + $("#movie_movieId").val(),
                            onSubmit: function () {
                                if ($("#movieAddForm").form("validate")) {
                                    $.messager.progress({
                                        text: "正在提交数据中...",
                                    });
                                    return true;
                                } else {
                                    return false;
                                }
                            },
                            success: function () {
                                $.messager.progress("close");
                                $.messager.alert("消息", "信息修改成功！");
                                $("#movieAddDiv").dialog("close");
                                movie_manage_tool.reload();
                            }
                        });
                        $("#movie_movieDesc").text(tinyMCE.editors['movie_movieDesc'].getContent());
                        //提交表单
                        $("#movieAddForm").submit();
                    }
                }
            },
        }, {
            text: "取消",
            iconCls: "icon-redo",
            handler: function () {
                $("#movieAddDiv").dialog("close");
                $("#movieAddForm").form("reset");
            },
        }],
    });

    $("#movieRatingDiv").dialog({
        title: "影片评分",
        top: "10px",
        width: 1000,
        height: 600,
        modal: true,
        closed: true,
        iconCls: "icon-edit-new",
        buttons: [{
            text: "提交",
            iconCls: "icon-edit-new",
            handler: function () {
                if ($("#movieRatingForm").form("validate")) {
                    if (!$("#movieRatingForm").form("validate")) {
                        $.messager.alert("错误提示", "你输入的信息还有错误！", "warning");
                    } else {
                        $("#movieRatingForm").form({
                            url: "/Movie/rating/" + $("#rating_movieId").val(),
                            onSubmit: function () {
                                if ($("#movieRatingForm").form("validate")) {
                                    $.messager.progress({
                                        text: "正在提交数据中...",
                                    });
                                    return true;
                                } else {
                                    return false;
                                }
                            },
                            success: function () {
                                $.messager.progress("close");
                                $.messager.alert("消息", '添加成功！');
                                $("#movieRatingDiv").dialog("close");
                                movie_manage_tool.reload();
                            },

                        });
                        $("#movieRatingForm").submit();
                    }
                }
            },
        }, {
            text: "取消",
            iconCls: "icon-redo",
            handler: function () {
                $("#movieRatingDiv").dialog("close");
                $("#movieRatingForm").form("reset");
            },
        }],
    });
});

function initMovieManageTool() {
    movie_manage_tool = {
        reload: function () {
            $("#movie_manage").datagrid("reload");
        },
        redo: function () {
            $("#movie_manage").datagrid("unselectAll");
        },
        search: function () {
            var queryParams = $("#movie_manage").datagrid("options").queryParams;
            queryParams["title"] = $("#title").val();
            queryParams["genres"] = $("#genres").val();
            queryParams["movieYear"] = $("#movieYear").val();
            queryParams["csrfmiddlewaretoken"] = $('input[name="csrfmiddlewaretoken"]').val();
            $("#movie_manage").datagrid("options").queryParams = queryParams;
            $("#movie_manage").datagrid("load");
        },
        remove: function () {
            var rows = $("#movie_manage").datagrid("getSelections");
            if (rows.length > 0) {
                $.messager.confirm("确定操作", "您正在要删除所选的记录吗？", function (flag) {
                    if (flag) {
                        var movies = [];
                        for (var i = 0; i < rows.length; i++) {
                            movies.push(rows[i].movieId);
                        }
                        $.ajax({
                            type: "POST",
                            url: "/Movie/delete",
                            data: {
                                movieIds: movies.join(","),
                                "csrfmiddlewaretoken": $('input[name="csrfmiddlewaretoken"]').val()
                            },
                            beforeSend: function () {
                                $("#movie_manage").datagrid("loading");
                            },
                            success: function (data) {
                                if (data.success) {
                                    $("#movie_manage").datagrid("loaded");
                                    $("#movie_manage").datagrid("load");
                                    $("#movie_manage").datagrid("unselectAll");
                                    $.messager.alert("提示", data.message);
                                    $(".messager-window").css("z-index", 10000);
                                } else {
                                    $("#movie_manage").datagrid("loaded");
                                    $("#movie_manage").datagrid("load");
                                    $("#movie_manage").datagrid("unselectAll");
                                    $.messager.alert("消息", data.message);
                                }
                            },
                        });
                    }
                });
            } else {
                $.messager.alert("提示", "请选择要删除的记录！", "info");
            }
        },
        edit: function () {
            var rows = $("#movie_manage").datagrid("getSelections");
            if (rows.length > 1) {
                $.messager.alert("警告操作！", "编辑记录只能选定一条数据！", "warning");
            } else if (rows.length == 1) {
                $.ajax({
                    url: "/Movie/update/" + rows[0].movieId,
                    type: "get",
                    data: {},
                    beforeSend: function () {
                        $.messager.progress({
                            text: "正在获取中...",
                        });
                    },
                    success: function (movie, response, status) {
                        $.messager.progress("close");
                        if (movie) {
                            $("#movieAddDiv").dialog("open");
                            $("#movie_movieId").val(movie.movieId);
                            $("#movie_title").val(movie.title);
                            $("#movie_genres").combobox({
                                url: "/Movie/genreAll?csrfmiddlewaretoken=" + $('input[name="csrfmiddlewaretoken"]').val(),
                                method: "GET",
                                valueField: "movieGenresId",
                                textField: "movieGenresName",
                                panelHeight: "auto",
                                editable: false,
                                onLoadSuccess: function () { //数据加载完毕事件
                                    $("#movie_genres").combobox("select", movie.genres);
                                }
                            });
                            $("#movie_movieYear").val(movie.movieYear);
                            $("#movie_movieYear").validatebox({
                                required: true,
                                missingMessage: "请输入影片上映年份",
                            });
                            tinyMCE.editors['movie_movieDesc'].setContent(movie.movieDesc);
                        } else {
                            $.messager.alert("获取失败！", "未知错误导致失败，请重试！", "warning");
                        }
                    }
                });
            } else if (rows.length == 0) {
                $.messager.alert("警告操作！", "编辑记录至少选定一条数据！", "warning");
            }
        },
        rating: function () {
            var rows = $("#movie_manage").datagrid("getSelections");
            if (rows.length > 1) {
                $.messager.alert("警告操作！", "评分只能选定一条数据！", "warning");
            } else if (rows.length == 1) {
                $.ajax({
                    url: "/Movie/rating/" + rows[0].movieId,
                    type: "get",
                    data: {},
                    beforeSend: function () {
                        $.messager.progress({
                            text: "正在获取中...",
                        });
                    },
                    success: function (movie, response, status) {
                        $.messager.progress("close");
                        if (movie) {
                            $("#movieRatingDiv").dialog("open");
                            $("#rating_movieId").val(movie.movieId);
                            $("#rating_title").val(movie.title);
                        } else {
                            $.messager.alert("获取失败！", "未知错误导致失败，请重试！", "warning");
                        }
                    }
                });
            } else if (rows.length == 0) {
                $.messager.alert("警告操作！", "评分至少选定一条数据！", "warning");
            }
        },
    };
}
